import axios from 'axios'
import {commonParams, url, postUrl} from './config'

export function getExpertList() {
  const data = Object.assign({}, commonParams, {
    do: 'doctor'
  })
  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}

export function getHealthList() {
  const data = Object.assign({}, commonParams, {
    do: 'package'
  })
  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}

export function getHealthDetail(id) {
  const data = Object.assign({}, commonParams, {
    do: 'package',
    packageId: id
  })
  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}

export function getNewsList() {
  const data = Object.assign({}, commonParams, {
    do: 'news'
  })
  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}

export function getNewsDetail(id) {
  const data = Object.assign({}, commonParams, {
    do: 'news',
    newsId: id
  })
  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}

export function getCenterDetail() {
  const data = Object.assign({}, commonParams, {
    do: 'homeConfig',
  })
  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}

export function payUrl(orderId) {
  let payUrl = url + '?'
  for (let key in commonParams) {
    payUrl += `${key}=${commonParams[key]}&`
  }
  payUrl += `do=order&order_id=${orderId}&iPay=1`
  return payUrl
}

export function http(api, datain, method) {

  const data = Object.assign({}, commonParams, datain, {
    do: api,
  })

  if (method === 'post') {
    let postUrl = url + '?'
    for (let key in commonParams) {
      postUrl += `${key}=${commonParams[key]}&`
    }
    postUrl += `do=${api}`

    const qs = require('qs');

    return axios.post(postUrl, qs.stringify(data)).then((res) => {
      return Promise.resolve(res.data)
    })
  } else {
    return axios.get(url, {
      params: data
    }).then((res) => {
      return Promise.resolve(res.data)
    })
  }

}
